<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
    <SearchForm
      :searchData="pagination"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
      @handleClear="handleClear"
    ></SearchForm>
    <TableList
      :data="data"
      :total="total"
      :pagination="pagination"
      @getCurrent="getCurrent"
      @handleBulid="handleBulid"
      @handleEdit="handleEdit"
      @handleClickDelete="handleClickDelete"
      @handleForbidden="handleForbidden"
    >
    </TableList>

    <DialogFrom
      ref="sunCom"
      :visible="visible"
      :planData="planData"
      :title="title"
      :data="fromBaseData"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEditForm="handleEditForm"
    ></DialogFrom>

    <DELETE
      :visible="dialogDeleteVisible"
      :delete-text="operateText"
      @handle-delete="handleDelete"
      @handle-close="handleCloseDelete"
    ></DELETE>
    <!-- 禁用弹层 -->
    <Forbidden
      :visible="dialogVisible"
      :text="operateText"
      @handle-submit="handleForbiddenSub"
      @handle-close="handleForbiddenClose"
    ></Forbidden>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import {
  getLevelDetails,
  getLevelList,
  getnursAllPlanList,
  levelAdd,
  levelDelete,
  levelStatus,
  levelUpdate
} from '@/api/serve'
import TableList from './components/TableList.vue'
import SearchForm from './components/SearchForm.vue'
import DialogFrom from './components/DialogFrom.vue'
import DELETE from '@/components/OperateDialog/index.vue'
import Forbidden from '@/components/Forbidden/index.vue'

const data = ref([])
// 分页总记录数
const total = ref(0)
// 分页参数
const pagination = ref({
  pageNum: 1,
  pageSize: 10,
  name: '',
  status: ''
})
const getAll = async () => {
  const res = await getLevelList(pagination.value)
  if (res.code === 200) {
    data.value = res.data.records
    total.value = res.data.total
  }
}
// 翻页
const getCurrent = (pageInfo) => {
  pagination.value.pageNum = pageInfo.current
  pagination.value.pageSize = pageInfo.pageSize
  getAll()
}
onMounted(() => {
  getAll()
  getPlan()
})
// 搜索功能
const handleSearch = () => {
  pagination.value.pageNum = 1
  getAll()
}
// 重置，清空搜索框
const handleReset = () => {
  // 重置页码
  pagination.value = {
    pageSize: 10,
    pageNum: 1
  }
  getAll()
}
// 清空数据重新查询
const handleClear = (val) => {
  pagination.value.pageNum = 1
  // 判断是否为name
  if (val === 'name') {
    pagination.value.name = ''
  } else if (val === 'status') {
    pagination.value.status = ''
  } else {
    pagination.value.name = ''
    pagination.value.status = ''
  }
  getAll()
}
// 新增
const visible = ref(false)
const title = ref('')
const planData = ref([])
const fromBaseData = ref({})
const handleBulid = () => {
  visible.value = true
  title.value = '新增'
  getPlan()
}
const handleClose = () => {
  visible.value = false
}
const getPlan = async () => {
  const res = await getnursAllPlanList()

  if (res.code === 200) {
    planData.value = res.data
  }
}
const handleEdit = (val) => {
  visible.value = true
  title.value = '修改'
  getLevelById(val.id)
}
const getLevelById = async (id) => {
  const res: any = await getLevelDetails(id)
  if (res.code === 200) {
    fromBaseData.value = res.data
  }
}
const sunCom = ref(null)
const handleAdd = async (val) => {
  const res: any = await levelAdd(val)
  if (res.code === 200) {
    MessagePlugin.success('添加成功')
    getAll()
    handleClose()
    sunCom.value.handleClear()
  } else {
    MessagePlugin.success('添加失败')
  }
}
const handleEditForm = async (val) => {
  const res: any = await levelUpdate(val)
  if (res.code === 200) {
    MessagePlugin.success('修改成功')
    getAll()
    handleClose()
    sunCom.value.handleClear()
  } else {
    MessagePlugin.success('修改失败')
  }
}
// 删除
const dialogDeleteVisible = ref(false)
const operateText = ref('护理项目')
// 点击删除
const handleClickDelete = (row) => {
  dialogDeleteVisible.value = true
  id.value = row.id
}
const id = ref(0)
const handleDelete = async () => {
  const res = await levelDelete(id.value)
  if (res.code === 200) {
    MessagePlugin.success('删除成功')
    handleCloseDelete()
    getAll()
  } else {
    MessagePlugin.error('删除失败')
  }
}
// 点击删除关闭
const handleCloseDelete = () => {
  dialogDeleteVisible.value = false
}

// 启用禁用
const typeId = ref(0)
const typeStatus = ref(0)
const statusText = ref('')
const dialogVisible = ref(false)
const handleForbidden = (val) => {
  typeId.value = val.id
  if (val.status === 1) {
    dialogVisible.value = true
    typeStatus.value = 0
    statusText.value = '禁用成功'
  } else {
    typeStatus.value = 1
    handleForbiddenSub()
    statusText.value = '启用成功'
  }
}
// 确定禁用
const handleForbiddenSub = async () => {
  const params = {
    id: typeId.value,
    status: typeStatus.value
  }
  const res: any = await levelStatus(params)
  if (res.code === 200) {
    dialogVisible.value = false
    MessagePlugin.success(statusText.value)
    getAll()
  }
}
</script>
